package cn.hhz._05weibo.controller;

import cn.hhz._05weibo.common.response.JsonResult;
import cn.hhz._05weibo.common.response.StatusCode;
import cn.hhz._05weibo.mapper.WeiboMapper;
import cn.hhz._05weibo.pojo.dto.WeiboDTO;
import cn.hhz._05weibo.pojo.entity.Weibo;
import cn.hhz._05weibo.pojo.vo.UserV0;
import cn.hhz._05weibo.pojo.vo.WeiboDetailVO;
import cn.hhz._05weibo.pojo.vo.WeiboIndexVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Validated
@Api(tags = "02.微博模块")
@RestController
@RequestMapping("v1/weibo/")
public class WeiboController {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    WeiboMapper weiboMapper;

    @ApiOperation("微博发表功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        UserV0 userV0 = (UserV0) session.getAttribute("user");
        if (userV0 == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUser_id(userV0.getId());
        System.out.println(weibo);
        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }

    @ApiOperation("微博首页展示")
    @GetMapping("selectIndex")
    public JsonResult selectIndex() throws JsonProcessingException {
        ValueOperations<String,Serializable> ops = redisTemplate.opsForValue();
        Serializable weiboindex = ops.get("weiboindex");
        if(weiboindex ==null){
            List<WeiboIndexVO> weiboIndexVOS = weiboMapper.selectIndex();
            ObjectMapper objectMapper = new ObjectMapper();
            String indexjson = objectMapper.writeValueAsString(weiboIndexVOS);
            ops.set("weiboindex",indexjson,30, TimeUnit.SECONDS);
            System.out.println("首页来自mysql");
            return JsonResult.ok(weiboIndexVOS);
        }
        System.out.println("来自radis");
        ObjectMapper objectMapper = new ObjectMapper();
        List<WeiboIndexVO> list = objectMapper.readValue(weiboindex.toString(), List.class);
        return JsonResult.ok(list);
    }

    @ApiOperation("微博详情功能")
    @GetMapping("selectById")
    @ApiImplicitParam(name = "id", value = "微博编号", required = true, dataType = "int", example = "200")
    public JsonResult selectById(@Range(min=100,max=500,message = "微博ID不符") @RequestParam int id) {
        if(id<0){
            throw new IllegalArgumentException("微博ID不能小于0");
//            return new JsonResult(StatusCode.OPERATION_FAILED,"ID值不能小于0");
        }
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }

}
